package cn.org.eshow.webapp.action.h5;

import cn.org.eshow.bean.query.ProductOrderQuery;
import cn.org.eshow.model.Money;
import cn.org.eshow.model.ProductOrder;
import cn.org.eshow.service.MoneyManager;
import cn.org.eshow.service.ProductOrderManager;
import cn.org.eshow.webapp.action.BaseAction;
import cn.org.eshow.webapp.util.RenderUtil;
import cn.org.eshow.webapp.util.Struts2Utils;
import com.pingplusplus.model.Charge;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 商品订单 H5接口
 */
@Results({@Result(name = "input", location = "add"),
        @Result(name = "list", type = "redirect", location = "/productOrder/"),
        @Result(name = "success", type = "redirect", location = "view/${id}"),
        @Result(name = "redirect", type = "redirect", location = "${redirect}")})
@AllowedMethods({"pay", "view", "comment"})
public class ProductOrderAction extends BaseAction {

    private static final long serialVersionUID = 1L;

    @Autowired
    private ProductOrderManager productOrderManager;
    @Autowired
    private MoneyManager moneyManager;

    private ProductOrder productOrder = new ProductOrder();
    private List<ProductOrder> productOrders = new ArrayList<ProductOrder>();
    private ProductOrderQuery query = new ProductOrderQuery(Boolean.TRUE);

    public String view() {
        productOrder = productOrderManager.get(id);
        return NONE;
    }

    /**
     * 支付方式选择及支付接口
     */
    public void pay() {
        if (id == null) {
            RenderUtil.failure("缺少参数");
            return;
        }
        if (productOrder.getPayment() == null) {
            RenderUtil.failure("请选择支付方式");
            return;
        }
        ProductOrder old = productOrderManager.get(id);

        if (0 == productOrder.getPayment() || 1 == productOrder.getPayment()) {
            //0 支付宝支付，1微信支付
            old.setUpdateTime(new Date());
            old.setPayment(productOrder.getPayment());
            old = productOrderManager.save(old);

            Struts2Utils.renderText("{\"status\":\"1\",\"msg\":\"" + old.getId().toString() + "\",\"" + "orderNo" + "\":\"" + old.getNo() + "\"}");
            return;
        } else if (2 == productOrder.getPayment()) {
            //2 余额支付
            Money myMoney = moneyManager.mine(old.getUser());
            if (myMoney.getBalance() < old.getPrice()) {
                RenderUtil.failure("余额不足");
                return;
            }
            old.setPayment(productOrder.getPayment());
            old = productOrderManager.save(old);

            moneyManager.frozen(myMoney, old.getPrice());
            moneyManager.balance(myMoney, -old.getPrice());

            Charge charge = new Charge();
            charge.setOrderNo(old.getNo());
            charge.setId("余额支付");
            productOrderManager.pay(charge);

            RenderUtil.success("支付成功");
            return;
        }
        RenderUtil.failure("支付失败，请选择支付方式");
    }




    public ProductOrder getProductOrder() {
        return productOrder;
    }

    public void setProductOrder(ProductOrder productOrder) {
        this.productOrder = productOrder;
    }

    public List<ProductOrder> getProductOrders() {
        return productOrders;
    }

    public void setProductOrders(List<ProductOrder> productOrders) {
        this.productOrders = productOrders;
    }

    public ProductOrderQuery getQuery() {
        return query;
    }

    public void setQuery(ProductOrderQuery query) {
        this.query = query;
    }


}